using UnityEngine;
using System.Collections;

public class FPSController : MonoBehaviour 
{
	private Vector3 lastPosition;

    FirstPersonView m_View;

    public Vector3 m_velocity;
    public float m_GroundVelocity;
    public float m_AirVelocity;

    public float m_ValVelocity;

	bool inAir;			// Détermine si le personnage se trouve en l'air
	bool jump;			// Permet de déterminer si le joueur peut ou non sauter
	
	int nbCollision;	// Nombre d'objet entrant en collision avec le personnage
	
	public float jumpStrenght			=100.0f;
	
	public float velocityForwardMax		=100.0f;
	public float velocityLeftMax		=100.0f;
	public float velocityRightMax		=100.0f;
	public float velocityBackwardMax	=100.0f;
	
	public float throttleForward;
	public float throttleBackward;
	public float throttleLeft;
	public float throttleRight;
	
	private Vector3 direction;
	private Vector3 directionRight;
	private float airCoef;
	
	// Use this for initialization

    void Awake()
    {
       
    }

	void Start ()
    {
        m_View = Character.Instance.m_View;
		airCoef=1.0f;
		
		jump=false;
        m_ValVelocity = 0.6f;
        m_GroundVelocity = 0.6f;
        m_AirVelocity = 0.03f;

		jumpStrenght	=	3.0f;
		throttleForward=30.0f;
		throttleBackward=30.0f;
		throttleLeft=30.0f;
		throttleRight=30.0f;
		
		velocityForwardMax		=15.0f;
		direction=new Vector3(m_View.transform.forward.x,
								0.0f, m_View.transform.forward.z);
		
		directionRight=new Vector3(m_View.transform.right.x,
								0.0f, m_View.transform.right.z);
		
		
		
	}
	
	void FixedUpdate()
	{	
		if(inAir)
		{
			m_ValVelocity=m_AirVelocity;	
		}
		else
		{
            m_ValVelocity = m_GroundVelocity;
		}

		direction=new Vector3(m_View.transform.forward.x,
								0.0f, m_View.transform.forward.z);
		
		directionRight=new Vector3(m_View.transform.right.x,
								0.0f, m_View.transform.right.z);
		
		direction.Normalize();
		directionRight.Normalize();
		lastPosition=transform.position;

        bool  buttonPushed = false;
		
		if((Input.GetKey(KeyCode.Z) || (Input.GetKey(KeyCode.UpArrow))))
		{
            buttonPushed = true;
            Vector3 val = m_ValVelocity * direction;
            rigidbody.velocity = rigidbody.velocity + val;
		}
		if((Input.GetKey(KeyCode.S)|| (Input.GetKey(KeyCode.DownArrow))))
		{
            buttonPushed = true;
            Vector3 val = -m_ValVelocity * direction;
            rigidbody.velocity = rigidbody.velocity + val;
		}
		if((Input.GetKey(KeyCode.Q)|| (Input.GetKey(KeyCode.LeftArrow))))
		{
             buttonPushed = true;
            Vector3 val = -m_ValVelocity * directionRight;
            rigidbody.velocity = rigidbody.velocity + val;
		}
		if((Input.GetKey(KeyCode.D)|| (Input.GetKey(KeyCode.RightArrow))))
		{
             buttonPushed = true;
            Vector3 val = m_ValVelocity * directionRight;
            rigidbody.velocity = rigidbody.velocity + val;
		}
     
		if(rigidbody.velocity.magnitude>velocityForwardMax && !inAir )
		{
			rigidbody.velocity=rigidbody.velocity.normalized*velocityForwardMax;	
		}
       

        if (buttonPushed == false && !inAir)
        {
            
            rigidbody.velocity = rigidbody.velocity - direction * m_ValVelocity;
            float signeX = rigidbody.velocity.x;
            if (signeX >= 0)
            {
                signeX = 1;
            }
            else
            {
                signeX = -1;
            }

            float signeZ = rigidbody.velocity.z;

            if (signeZ >= 0)
            {
                signeZ = 1;
            }
            else
            {
                signeZ = -1;
            }

            if( (rigidbody.velocity.x)*(signeX)>=0.0f)
            {
                rigidbody.velocity= new Vector3(0.0f, rigidbody.velocity.y, rigidbody.velocity.z);
            }
             if((rigidbody.velocity.z*signeZ)>=0.0f)
            {
                rigidbody.velocity = new Vector3(rigidbody.velocity.x, rigidbody.velocity.y, 0.0f);
            }

        }
	
	}
	
	// Update is called once per frame
	void Update () 
	{
        if (Input.GetKeyDown(KeyCode.Space) && jump)
        {
            rigidbody.velocity=rigidbody.velocity+(Vector3.up * jumpStrenght * 2.5f);
        }
		
	}
	
	public void OnCollisionEnter(Collision collision)
	{
		// Si je me trouve "sur" l'objet
		if(lastPosition.y>collision.contacts[0].otherCollider.transform.position.y)
		{
			jump=true;
			inAir=false;
		}
		else
		{
			
		}
		nbCollision++;	
		
	}
	
	public void OnCollisionExit(Collision collision)
	{
		nbCollision--;
		
		if(nbCollision==0)
		{
			inAir=true;
			jump=false;
		}
	}
}
